<template>
  <div class="meeting-room-management-page">
    <h2>会议室管理</h2>
    
    <!-- 添加会议室按钮 -->
    <el-button type="primary" @click="showAddDialog">添加会议室</el-button>

    <!-- 会议室列表 -->
    <el-table :data="meetingRooms" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="roomId" label="ID" width="80" />
      <el-table-column prop="roomname" label="会议室名称" width="120" />
      <el-table-column label="类型" width="100">
        <template #default="{ row }">
          {{ getRoomType(row.roomType) }}
        </template>
      </el-table-column>
      <el-table-column prop="capacity" label="容纳人数" width="100" />
      <el-table-column prop="area" label="面积" width="100">
        <template #default="{ row }">
          {{ row.area }}㎡
        </template>
      </el-table-column>
      <el-table-column label="设备状态" width="300">
        <template #default="{ row }">
          <el-tag :type="row.projector === 1 ? 'success' : 'danger'" style="margin-right: 5px">
            投影仪{{ row.projector === 1 ? '正常' : '异常' }}
          </el-tag>
          <el-tag :type="row.sound === 1 ? 'success' : 'danger'" style="margin-right: 5px">
            音响{{ row.sound === 1 ? '正常' : '异常' }}
          </el-tag>
          <el-tag :type="row.internet === 1 ? 'success' : 'danger'">
            网络{{ row.internet === 1 ? '正常' : '异常' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="价格/小时" width="120">
        <template #default="{ row }">
          ¥{{ row.price }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)">
            {{ getRoomStatus(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button size="small" type="primary" @click="handleEdit(row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑会议室对话框 -->
    <el-dialog
      :title="dialogType === 'add' ? '添加会议室' : '编辑会议室'"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form :model="roomForm" :rules="rules" ref="roomFormRef" label-width="100px">
        <el-form-item label="会议室名称" prop="roomname">
          <el-input v-model="roomForm.roomname" placeholder="请输入会议室名称" />
        </el-form-item>
        <el-form-item label="会议室类型" prop="roomType">
          <el-select v-model="roomForm.roomType" placeholder="请选择会议室类型">
            <el-option label="教室" :value="1" />
            <el-option label="圆桌" :value="2" />
            <el-option label="董事会" :value="3" />
            <el-option label="多媒体" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="容纳人数" prop="capacity">
          <el-input-number v-model="roomForm.capacity" :min="1" :max="100" />
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input-number v-model="roomForm.area" :min="1" :precision="2" :step="1" />
          <span class="unit">㎡</span>
        </el-form-item>
        <el-form-item label="设备状态">
          <el-checkbox v-model="roomForm.projector" :true-value="1" :false-value="0">投影仪正常</el-checkbox>
          <el-checkbox v-model="roomForm.sound" :true-value="1" :false-value="0">音响正常</el-checkbox>
          <el-checkbox v-model="roomForm.internet" :true-value="1" :false-value="0">网络正常</el-checkbox>
        </el-form-item>
        <el-form-item label="价格/小时" prop="price">
          <el-input-number v-model="roomForm.price" :min="0" :precision="2" :step="10" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="roomForm.status" placeholder="请选择状态">
            <el-option label="未使用" :value="0" />
            <el-option label="使用中" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 删除确认对话框 -->
    <el-dialog
      title="确认删除"
      v-model="deleteDialogVisible"
      width="300px"
    >
      <p>确定要删除该会议室吗？此操作不可恢复。</p>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="deleteDialogVisible = false">取消</el-button>
          <el-button type="danger" @click="confirmDelete">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { getAllRooms, createRoom, updateRoom } from '@/apis/room';

const meetingRooms = ref([]);
const dialogVisible = ref(false);
const dialogType = ref('add');
const roomFormRef = ref(null);
const currentRoom = ref(null);

const roomForm = ref({
  roomname: '',
  roomType: 1,
  capacity: 1,
  area: 20,
  projector: 1,
  sound: 1,
  internet: 1,
  price: 0,
  status: 0
});

const rules = {
 roomname: [
    { required: true, message: '请输入会议室名称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  roomType: [
    { required: true, message: '请选择会议室类型', trigger: 'change' }
  ],
  capacity: [
    { required: true, message: '请输入容纳人数', trigger: 'blur' }
  ],
  area: [
    { required: true, message: '请输入会议室面积', trigger: 'blur' }
  ],
  price: [
    { required: true, message: '请输入价格', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
};

// 获取所有会议室
const fetchMeetingRooms = async () => {
  try {
    const response = await getAllRooms();
    if (response.code === 200) {
      meetingRooms.value = response.data;
    } else {
      ElMessage.error(response.msg || '获取会议室列表失败');
    }
  } catch (error) {
    console.error('获取会议室列表出错:', error);
    ElMessage.error('获取会议室列表失败');
  }
};

// 显示添加对话框
const showAddDialog = () => {
  dialogType.value = 'add';
  roomForm.value = {
    roomname: '',
    roomType: 1,
    capacity: 1,
    area: 20,
    projector: 1,
    sound: 1,
    internet: 1,
    price: 0,
    status: 0
  };
  dialogVisible.value = true;
};

// 显示编辑对话框
const handleEdit = (row) => {
  dialogType.value = 'edit';
  currentRoom.value = row;
  roomForm.value = {
    ...row,
    projector: Number(row.projector),
    sound: Number(row.sound),
    internet: Number(row.internet)
  };
  dialogVisible.value = true;
};

// 提交表单
const handleSubmit = async () => {
  if (!roomFormRef.value) return;
  
  await roomFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const formData = {
          ...roomForm.value,
          projector: Number(roomForm.value.projector),
          sound: Number(roomForm.value.sound),
          internet: Number(roomForm.value.internet),
          status: Number(roomForm.value.status)
        };

        let response;
        if (dialogType.value === 'add') {
          response = await createRoom(formData);
        } else {
          response = await updateRoom(currentRoom.value.roomId, formData);
        }

        if (response.code === 200) {
          ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功');
          dialogVisible.value = false;
          await fetchMeetingRooms();
        } else {
          ElMessage.error(response.msg || (dialogType.value === 'add' ? '添加失败' : '更新失败'));
        }
      } catch (error) {
        console.error('提交表单出错:', error);
        ElMessage.error(dialogType.value === 'add' ? '添加失败' : '更新失败');
      }
    }
  });
};

// 获取会议室类型文本
const getRoomType = (type) => {
  const types = {
    1: '教室',
    2: '圆桌',
    3: '董事会',
    4: '多媒体'
  };
  return types[type] || '未知类型';
};

// 获取状态文本
const getRoomStatus = (status) => {
  const statuses = {
    0: '未使用',
    1: '使用中'
  };
  return statuses[status] || '未知状态';
};

// 获取状态标签类型
const getStatusType = (status) => {
  const types = {
    0: 'success',
    1: 'warning'
  };
  return types[status] || '';
};

onMounted(() => {
  fetchMeetingRooms();
});
</script>

<style scoped>
.meeting-room-management-page {
  padding: 2rem;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

:deep(.el-tag) {
  margin: 2px;
}

.unit {
  margin-left: 5px;
  color: #606266;
  font-size: 14px;
}
</style> 